package com.web.controller;

import com.web.entity.PageVO;
import com.web.entity.ResponseResult;
import com.web.entity.Student;
import com.web.service.StudentService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: XiangHu
 * @date: 2025年03月20日 17:36
 * @describe:
 */
@CrossOrigin
@RestController
public class StudentController {
    @Resource
    private StudentService studentService;
    @RequestMapping("/findstudents")
    public ResponseResult findStudents(){
        List<Student> students = studentService.findAllStudents();
        return new ResponseResult(200, "查询成功", students);
    }
@RequestMapping("findstudentpage")
    public ResponseResult findStudentPage(@RequestParam(name = "current",required = false,defaultValue = "1") int current,
                                          @RequestParam(name = "pageSize",required = false,defaultValue = "10") int pageSize){
    PageVO page = studentService.findStudentPage(current, pageSize);
        return new ResponseResult(200,"查询成功",page);
    }
    @RequestMapping("/insertstudent")
    public ResponseResult insertStudent(Student student){
        int i = studentService.addStudent(student);
        if (i > 0) {
            return new ResponseResult(200, "添加成功", null);
        } else {
            return new ResponseResult(400, "添加失败", null);
        }
    }
    @RequestMapping("/updatestudent")
    public ResponseResult updateStudent(Student student){
        int result = studentService.updateStudent(student);
        if (result > 0) {
            return new ResponseResult(200, "更新成功", student);
        } else {
            return new ResponseResult(400, "更新失败", null);
        }
    }
    @RequestMapping("/removestudent")
    public ResponseResult removeStudent(int id){
        int result = studentService.deleteStudent(id);
        if (result > 0) {
            return new ResponseResult(200, "删除成功", null);
        } else {
            return new ResponseResult(400, "删除失败", null);
        }
    }

}
